<!--
 * @description  : 市管-市场检查首页
 * @descriptionDetail: 市管-市场检查首页
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2022-09-06 17:57:44
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar
      :is-white="false"
      :title="navTitle"
      :custom-back="goBack"
      style="z-index: 99999"
    >
    </theme-navbar>
    <!-- #endif -->
    <view class="content u-page">
      <!-- 内容区 -->
      <u-image class="head-img" :width="getImgWidth()" src="/static/common/banner_op.png" mode="widthFix"></u-image>
      <view class="grid-lay">
        <u-grid :col="3" :border="false">
          <u-grid-item :index="index" v-for="(item,index) in list" :key="item.key" @click="gridClick">
            <u-image :src="item.iconPath" :width="100" mode="aspectFit" :height="100"></u-image>
            <view class="grid-text">
              {{ item.text }}
            </view>
          </u-grid-item>
          <u-grid-item>
            <u-image @tap="handlerScan" src="/static/check/round-scan.png" :width="100" mode="aspectFit" :height="100"></u-image>
            <view class="grid-text">
              执法证领用
            </view>
          </u-grid-item>
        </u-grid>
      </view>
    </view>
  </view>
</template>

<script>
import checkService from '@/service/check/check.service';
import {
  dateUtils
} from '@/common/util';
import {
  isDD,
  isWorkWechat,
  isEmpApp
} from '@/common/util';
import {
  SET_MAP_LAT_LON
} from '@/config/setting.config.js';
export default {
  components: {

  },
  data() {
    return {
      list:[
        {
          iconPath: "../../static/dashboard/scjc.png",
          text: '市场检查',
          key:0,
          url:'/pages/check/checkList',
        },
        {
          iconPath: "../../static/common/icon_tsqy.png",
          text: '特殊区域检查',
          key:1,
          url:'/pages/check/specialAreasCheckList',
        },
        {
          iconPath: "../../static/common/undocuEcigarette.png",
          text: '无证电子烟',
          key:2,
          url:'/pages/check/undocuEcigarette/undocuEcigaretteList',
        },
      ],
      taskInfo: {
        longitude: 0, // 经度
        latitude: 0, // 纬度
        signLongitude:0,
        signLatitude:0,
        orgUuid: '',
        deviateDistance:0,
        isDeviate:0
      }, // 检查任务详情
      longitude: 0, // 经度
      latitude: 0, // 纬度
    };
  },
  // 页面加载成功后也要设置一次，用于处理本页面刷新后失效问题
  mounted() {
    this.queryTaskInfo();
  },
  methods: {
    gridClick(index){
      if(this.list[index].url){
        this.setRouteParam(this.list[index].text);
        this.$u.route(this.list[index].url);
      }
    },
    getImgWidth(){
      return (this.$u.sys().windowWidth - uni.upx2px(32)) + 'px';
    },

    // 查询检查任务
    queryTaskInfo() {
      // debugger;
      this.$u.loading('数据加载中...');
      let checkRecord = '';
      checkRecord = checkService.getOrgInfoExt;
      checkRecord()
          .then((dr) => {
            this.$u.hideLoading();
            if (dr.success) {
              this.taskInfo = dr.bean;
              this.taskInfo.longitude = dr.bean.longitude;
              this.taskInfo.latitude = dr.bean.latitude;
              console.log("======",this.taskInfo);
            } else {
              this.$u.toast(dr.msg);
            }
          })
          .catch((err) => {
            this.$u.hideLoading();
            this.$u.toast(err);
          });
    },
    handlerScan() {
      let isHaveLngLat = false;
      this.getLocation();
      this.$u.loading('正在计算当前位置...');
      this.taskInfo.signLongitude = this.longitude;
      this.taskInfo.signLatitude = this.latitude;
      this.taskInfo.isDeviate = 0;
      // 计算距离
      if (this.taskInfo.longitude && this.taskInfo.latitude) {
        isHaveLngLat = true;
        let p1 = new AMap.LngLat(this.taskInfo.signLongitude, this.taskInfo.signLatitude);
        let p2 = new AMap.LngLat(this.taskInfo.longitude, this.taskInfo.latitude);
        this.taskInfo.deviateDistance = Math.round(p1.distance(p2));
        console.log('两点相距=' + this.taskInfo.deviateDistance);
        if (this.taskInfo.deviateDistance > 1000) {
          this.taskInfo.isDeviate = 1;
        }
      } else {
        // 没有坐标默认偏离
        this.taskInfo.isDeviate = 1;
      }
      this.$u.hideLoading();
      // 如果当前位置与零售位置相差1000米则提示
      if (this.taskInfo.isDeviate == 1 && isHaveLngLat) {
        this.$modal.confirm(
            '您当前位置距离专卖局' + this.taskInfo.deviateDistance + '米，是否继续领用？',
            '提示',
            () => {
              this.$deviceApi.scan().then((res) => {
                console.log("---扫描结果---"+res);
              });
            },
            () => {
              this.getLocation();
            },
            '确定',
            '稍后重试'
        );
      } else {
        this.$deviceApi.scan().then((res) => {

        });
      }
    },

    // 定位
    getLocation() {
      if (isDD() || isWorkWechat() || isEmpApp()) {
        this.$deviceApi.geolocation().then((geo) => {
          this.longitude = geo.longitude;
          this.latitude = geo.latitude;
        });
      } else {
        this.longitude = SET_MAP_LAT_LON[0];
        this.latitude = SET_MAP_LAT_LON[1];
      }
    },
    // 提交签到
    doSignIn() {
      this.$u.loading('正在签到...');
      this.taskInfo.signInTime = dateUtils.dateFormat('yyyy-MM-dd HH:mm:ss', new Date());
      let signTask = this.$u.deepClone(this.taskInfo);
      delete signTask.checkRecords;
      delete signTask.problemCigInfos;
      delete signTask.suspectedPeoples;
      delete signTask.relRetailers;
      delete signTask.reportComplaints;
      delete signTask.personInfos;
      delete signTask.storageInfos;
      delete signTask.vehicleInfoMts;
      delete signTask.supplySources;
      delete signTask.checkPicMts;
      delete signTask.checkDealResultVO;
      checkService
          .saveClientSign(signTask)
          .then((dr) => {
            this.$u.hideLoading();
            if (dr.success) {
              this.$u.toast('签到成功！');
              this.taskInfo.clientSign.status = M_DAY_CHECK_STATUS.SIGN;
              this.taskInfo.clientSign.statusName = '已签到';
              this.pageSignInTime = dateUtils.dateFormat('yyyy-MM-dd HH:mm:ss', new Date());
              this.taskInfo.clientSign.signInTime = dateUtils.dateFormat('yyyy-MM-dd HH:mm:ss', new Date());
              this.$forceUpdate();
            } else {
              this.$u.toast('签到失败: ' + dr.msg);
            }
          })
          .catch((err) => {
            this.$u.hideLoading();
            this.$u.toast(err);
          });
    },
  },
};
</script>

<style lang="scss" scoped>
.head-img{
  width: 100%;
  padding: 16rpx 16rpx;
  margin: 0 auto;
}
.grid-lay{
	padding: 0 32rpx;
}
.grid-text{
	color: $u-main-color;
	margin-top: 24rpx;
	font-size: 30rpx;
}
</style>
